<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:rx="http://www.renderx.com/XSL/Extensions"
    version="1.0">

    <xsl:template name="xep-document-information">
        <xsl:processing-instruction name="xep-pdf-ownerpassword"><xsl:value-of select="$doc.ownerpassword"/></xsl:processing-instruction>
        <xsl:processing-instruction name="xep-pdf-userprivileges">print,copy</xsl:processing-instruction>
        <xsl:processing-instruction name="xep-pdf-initial-zoom">fit-height</xsl:processing-instruction>
        <xsl:processing-instruction name="xep-pdf-linearize">true</xsl:processing-instruction>
        <rx:meta-info>
            <xsl:variable name="authors"
                select="(//authorinitials|//editor|//author|//corpauthor|//authorgroup)[1]" />
            <xsl:if test="$authors">
                <xsl:variable name="author">
                    <xsl:choose>
                        <xsl:when test="$authors[self::authorgroup]">
                            <xsl:call-template name="person.name.list">
                                <xsl:with-param name="person.list"
                                    select="$authors/*[self::author|self::corpauthor|self::othercredit|self::editor]"
                                 />
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="$authors[self::corpauthor]">
                            <xsl:value-of select="$authors" />
                        </xsl:when>
                        <xsl:when test="$authors[self::authorinitials]">
                            <xsl:value-of select="$authors" />
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="person.name">
                                <xsl:with-param name="node" select="$authors" />
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:element name="rx:meta-field">
                    <xsl:attribute name="name">author</xsl:attribute>
                    <xsl:attribute name="value">
                        <xsl:value-of select="normalize-space($author)" />
                    </xsl:attribute>
                </xsl:element>
            </xsl:if>

            <xsl:variable name="title">
                <xsl:apply-templates select="/*[1]" mode="label.markup" />
                <xsl:apply-templates select="/*[1]" mode="title.markup" />
            </xsl:variable>

            <xsl:element name="rx:meta-field">
                <xsl:attribute name="name">creator</xsl:attribute>
                <xsl:attribute name="value">
                    <xsl:text>Mercury custom layer for DocBook-XSL</xsl:text>
                </xsl:attribute>
            </xsl:element>

            <xsl:element name="rx:meta-field">
                <xsl:attribute name="name">title</xsl:attribute>
                <xsl:attribute name="value">
                    <xsl:value-of select="normalize-space($title)" />
                </xsl:attribute>
            </xsl:element>

            <xsl:if test="//keyword">
                <xsl:element name="rx:meta-field">
                    <xsl:attribute name="name">keywords</xsl:attribute>
                    <xsl:attribute name="value">
                        <xsl:for-each select="//keyword">
                            <xsl:value-of select="normalize-space(.)" />
                            <xsl:if test="position() != last()">
                                <xsl:text>, </xsl:text>
                            </xsl:if>
                        </xsl:for-each>
                    </xsl:attribute>
                </xsl:element>
            </xsl:if>

            <xsl:element name="rx:meta-field">
                <xsl:attribute name="name">subject</xsl:attribute>
                <xsl:attribute name="value">
                
                  <xsl:if test="//bookinfo/productname|//articleinfo/productname|//info[1]/productname">
                    <xsl:value-of select="normalize-space(//productname[1])" />
                  </xsl:if>
                  
                  <xsl:if test="//bookinfo/productnumber|//articleinfo/productnumber|//info[1]/productnumber">
                      <xsl:text> </xsl:text>
                      <xsl:value-of select="normalize-space(//productnumber[1])" />
                  </xsl:if>
                  
                  <xsl:if test="//subjectterm">
                      <xsl:text>, </xsl:text>
                      <xsl:for-each select="//subjectterm">
                          <xsl:value-of select="normalize-space(.)" />
                          <xsl:if test="position() != last()">
                              <xsl:text>, </xsl:text>
                          </xsl:if>
                      </xsl:for-each>
                  </xsl:if>
                  
                </xsl:attribute>
            </xsl:element>
        </rx:meta-info>
    </xsl:template>

</xsl:stylesheet>
